/**
 * 
 * @return
 */
var default_Option = {
	
	table_Id : "#listTable",
	head : [],
	searchURL : "",
	searchType : "json",
	$table : {},
	table_Option : {},
	$search : {},
	operation : {
		useable : true,
		edit : true,
		"delete" : true
	},
	queryFn : function(){
 	
		var $table = default_Option.$table,
			$search = default_Option.$search,
			pageSize = $("#QBar_Page").find(".current").attr("value"),
			pageNum = $table.getPageNum(),
			keyName = $("#QBar_SearchKey").find("a.current").attr("value"),
			keyValue = $("#QBar_Text").val().trim();
	
		$search.setPageNum(pageNum);
		$search.setPageSize(pageSize);
		$search.addCondition(keyName, keyValue);
		if($table.getOrderBy() && "operation" != $table.getOrderBy()){
			$search.addCondition($table.getOrderBy(), null, null,$table.getOrder());
		}
		$search.doSearch();
	},
	successFn : function(_result){

		var row;
		for(var i=0, len=_result.page.length; i<len; i+=1){
			row = _result.page[i];
			
			if(!default_Option.operation.useable) continue;
			
			if(default_Option.operation.edit){
				var $a = $("<a/>").attr("href", default_Option.editURL + "?id=" + row["id"]).html("[编辑]");
				row["operation"] = $a.prop("outerHTML");
			}
			
			if(default_Option.operation["delete"]){
				var $a = $("<a/>").attr("href", "javascript:default_Option.rowDeleteFn(" + row["id"] + ")").html("[删除]");
				row["operation"] += "&nbsp;" + $a.prop("outerHTML");
			}
		}
		this.$table.refresh(_result);
		$("#pageTotal").html(_result.total);
	},
	rowDeleteFn : function(_id){
		
		if (confirm("您确定要删除吗？")) {
		
			var url = default_Option.deleteURL,
			data = {id : _id},
			successFun = function(result){	//设置默认删除成功方法
				if(result.type == "success"){
					
					if(default_Option.callback_del(_id, result)) return;
					default_Option.queryFn();
					$.message(result);
				} else {
					$.message(result);
				}
			}
			
			$.post(url, data, successFun,"json");
		}
	},
	callback_del : function(_id, result){
	},
	addURL : "",
	deleteURL : "",
	editURL : "",
	execCmd : function(){this.queryFn();}
}

function initTable(_option){

	var option = $.extend(true, {}, default_Option),
		option = $.extend(true, option, _option),
		$table = QTable.init(option.table_Id, option.table_Option),
	 	$search = QSearch.init(option.searchURL, option.searchType);
	
	option.$search = $search;
	option.$table = $table;
	
	$search.onSearch(option.successFn);
	
	$table.refreshHead(option.head);

	$table.onOrderBy(function(_col){
		if(_col == "operation") return;
		$search.clear();
		option.queryFn();
	});
	
	$table.onPageSkip(function(p){$search.clear();option.queryFn();});	//页面跳转事件
	
	$table.onSelectedChange(function(){	//行选择更改事件
		if(this.getSelected().length > 0){
			$("#QBar_Del").removeClass("disabled");
		} else {
			$("#QBar_Del").addClass("disabled");
		}
	});

	$("#QBar_Add").attr("href", option.addURL);	//添加按钮事件
	
	$("#QBar_Del").click(function (){	//删除按钮事件
		var $this = $(event.currentTarget);
		if ($this.hasClass("disabled")) {
			return false;
		} else {
			$.dialog({
				type: "warn",
				content: "您确定要删除吗？",
				ok: "确定",
				cancel: "取消",
				onOk: function() {

					var table = QTable.get("#listTable"),
						url = option.deleteURL,
						data = {id : table.getSelected().join(",")},
						successFun = function(result){	//设置默认删除成功方法
							if(result.type == "success"){
								option.queryFn();
								$("#deleteButton").addClass("disabled");	//失效删除按钮
								$.message(result);
							} else {
								$.message(result);
							}
						};
					
					$.post(url, data, successFun,"json");
				}
			});
		}
	});

	$("#QBar_Refresh").click(function(){$search.clear(); option.queryFn();});
	$("#QBar_Query").click(function(){$search.clear(); option.queryFn();});
	$("#QBar_Text").keypress(function(){if(event.keyCode == 13){$search.clear(); option.queryFn();}});
	
	option.execCmd();
}